package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.pojo.AdminTable;
import com.neuedu.pojo.Result;
import com.neuedu.pojo.RoleTable;
import com.neuedu.service.RoleTableService;
import com.neuedu.mapper.RoleTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【role_table(角色信息表)】的数据库操作Service实现
* @createDate 2025-06-17 09:04:08
*/
@Service
public class RoleTableServiceImpl extends ServiceImpl<RoleTableMapper, RoleTable>
    implements RoleTableService{
    @Autowired
    private RoleTableMapper roleTableMapper;


    @Override
    public Result<List<RoleTable>> getRoleTables(Integer currentPage, Integer pageSize,String name) {
        // 设置条件
        Page<RoleTable> page = new Page<>(currentPage,pageSize);
        // 设置条件查询
        LambdaQueryWrapper<RoleTable> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),RoleTable::getRoleName,name)
                ;
        // 查询
        this.roleTableMapper.selectPage(page,wrapper);
        if (page.getRecords().size()>0){
            return Result.success(page.getRecords(),page.getTotal());
        }
        return Result.fail();
    }

    @Override
    public Result<Void> addRoleTable(RoleTable roleTable) {
        roleTable.setIsDelete(0);
        roleTable.setCreateTime(new Date());
        roleTable.setUpdateTime(new Date());
        // 添加
        int insert = this.roleTableMapper.insert(roleTable);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> updateRoleTable(RoleTable roleTable) {
        roleTable.setUpdateTime(new Date());
        // 添加
        int insert = this.roleTableMapper.updateById(roleTable);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> deleteRoleTable(Integer id) {
        LambdaUpdateWrapper<RoleTable> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(RoleTable::getIsDelete,1)
                .eq(RoleTable::getRoleId,id);
        int update = this.roleTableMapper.update(wrapper);
        if (update > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<List<RoleTable>> getRoleTables() {
        List<RoleTable> list = this.list();
        if(list.size()>0){
            return Result.success(list);
        }
        return Result.fail();
    }
}




